package vip.zhenzicheng.bio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * BIO 通信的服务端(优化版，使用线程池)
 *
 * @author zhenzicheng
 * @date: 2022-06-01 15:40
 */
public class ServerPool {
  // 创建固定数量线程池 线程数为cpu核心数量
  private static final ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());

  public static void main(String[] args) throws IOException {
    try (ServerSocket serverSocket = new ServerSocket()) {
      serverSocket.bind(new InetSocketAddress(10001));
      while (true) {
        executorService.execute(new Server.ServerTask(serverSocket.accept()));
      }
    }
  }
}
